package com.weipai.turnplate.service.impl;

import com.weipai.turnplate.enums.RetCode;
import com.weipai.turnplate.mapper.TurnplateMapper;
import com.weipai.turnplate.mapper.drawcodeMapper;
import com.weipai.turnplate.mapper.TurnplaterollcallMapper;
import com.weipai.turnplate.model.Turnplate;
import com.weipai.turnplate.model.Turnplaterollcall;
import com.weipai.turnplate.model.api.Response;
import com.weipai.turnplate.model.drawcode;
import com.weipai.turnplate.service.TurnplateService;
import com.weipai.turnplate.util.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * @Author: 赵项阳
 * @Date: 2019/6/1 11:57
 * @Version 1.0
 */
@Service

@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public class TurnplateServiceimpl implements TurnplateService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private TurnplaterollcallMapper turnplaterollcallMapper;
    @Autowired
    private TurnplateMapper turnplateMapper;
    @Autowired
    private drawcodeMapper drawcodeMapper;


    @Override
    public Response lottery(String drawcode, String name, String mobile, String address, String provinces) {
        Response response = new Response();

        try {
            List<drawcode>list=drawcodeMapper.selectByDrawCode(drawcode);
            if (list==null){
                response.setCode(RetCode.ERRORDRAWCODE);
                return response;
            }
            Turnplaterollcall turnplaterollcall = turnplaterollcallMapper.selectBymedMobile(mobile);
            if (turnplaterollcall != null) {
                response.setCode(RetCode.SORRY);
                return response;
            }

            Turnplate turnplate = turnplateMapper.selectByawardTitle(drawcode);
            if (turnplate != null) {

//                String grade = turnplateMapper.selectBydrawcode(drawcode);

                save(drawcode, address, name, mobile, provinces, response, turnplate, turnplate.getGrade());

                response.setCode(RetCode.SUCCESS);
            } else {
                response.setCode(RetCode.ERRORBYDRAWCODE);
                return response;
            }
        } catch (Exception e) {
            e.printStackTrace();

            logger.error("TurnplateServiceimpl.lottery!" + e.getMessage());
        }
        return response;
    }

    private void save(String drawcode, String address, String name, String mobile, String provinces, Response response, Turnplate turnplate, String grade) {
        response.setCode(RetCode.WINAPRIZE, turnplate.getLotteryName());
        String lotteryName = turnplate.getLotteryName();
        Turnplaterollcall turnplaterollcall1 = new Turnplaterollcall();
        turnplaterollcall1.setName(name);
        turnplaterollcall1.setLotteryName(lotteryName);
        turnplaterollcall1.setAddress(address);
        turnplaterollcall1.setMobile(mobile);
        turnplaterollcall1.setProvinces(provinces);
        turnplaterollcall1.setDrawCode(drawcode);
        turnplaterollcall1.setGrade(grade);
        turnplaterollcall1.setCreateTime(new Date());
        turnplaterollcall1.setGradeName(turnplate.getGradeName());
        turnplaterollcallMapper.insert(turnplaterollcall1);
    }

    @Override
    public Response updateByDrawCode(Integer[] id, String[] drawcode) {
        Response response = new Response();
        try {

            Turnplate turnplate = new Turnplate();
            drawcode drawcode1 = new drawcode();

            List<drawcode> list = drawcodeMapper.selectById(id);
            if (list.equals(drawcode)) {
                response.setCode(RetCode.UPDATErEPEAT);
                return response;
            }

            //修改抽奖码

            Date updateTime = new Date();

            drawcodeMapper.updatedrawcode(id,drawcode,updateTime);


            //判断修改后的抽奖码是否为空


                response.setCode(RetCode.SUCCESSUPDATE);

                return response;

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("TurnplateServiceimpl.updateByDrawCode!" + e.getMessage());
        }

        return response;
    }


    @Override
    public Response list(Integer page,Integer size,Integer visible) {

        Response response = new Response();
        try {
            
            Integer start = (page - 1) * size;
            List<Turnplate> list = turnplateMapper.selectByVisible(visible,start,size);
            List<Turnplaterollcall> list1 = turnplaterollcallMapper.selectByVisible(visible,start,size);
            List<drawcode> list2 = drawcodeMapper.selectByVisible(visible);
            response.put("list", list);
            response.put("list1", list1);
            response.put("list2", list2);

            response.setCode(RetCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("TurnplateServiceimpl.list" + e.getMessage());
        }
        return response;
    }

    @Override
    public Response save(drawcode drawcode) {
        Response response = new Response();
        try {

            //根据奖品id查询抽奖码记录
            List<Turnplate> list1 = drawcodeMapper.selectByPrizeid(drawcode.getPrizeid());

            if (list1 != null && !list1.equals("")) {

                Date updateTime = new Date();
                drawcode drawcode1 = new drawcode();
                drawcode1.setPrizeid(drawcode.getPrizeid());
                drawcode1.setDrawcode(drawcode.getDrawcode());
                drawcode1.setUpdateTime(updateTime);
                drawcode1.setVisible(1);

                drawcodeMapper.save(drawcode1);
                //查询抽奖码是否重复
                List<drawcode> listname = drawcodeMapper.selectByDrawCode1(drawcode.getDrawcode());
                if (listname.size() > 1) {

                    drawcodeMapper.deleteByPrimaryKey(drawcode.getDrawcode());
                    response.setCode(RetCode.DELETEDATA);
                    return response;

                }

                List<drawcode> list = drawcodeMapper.selectByVisible(drawcode.getVisible());
                response.put("list", list);
                response.setCode(RetCode.SUCCESSSAVE);
            } else {
                response.setCode(RetCode.ADDFAILED);
                return response;
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("TurnplateServiceimpl.save" + e.getMessage());
        }

        return response;
    }

    @Override
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Response updatebatch(List<drawcode> drawcode) {
        Response response = new Response();
        try {
            for (int i = 0; i < drawcode.size(); i++) {
                //Map<String, String> map = (Map<String, String>) drawcode.get(i);
//                drawcode drawcode1 = drawcode.get(i);
//                for (drawcode drawcode2 : drawcode) {
//                    Integer id=drawcode2.getId();
//                    String drawCode=drawcode1.getDrawcode();
                Integer id = drawcode.get(i).getId();
                String drawcode1 = drawcode.get(i).getDrawcode();
                List<drawcode> list = drawcodeMapper.selectByDrawCode(drawcode1);
                if (list.size()>1){
                    response.setCode(RetCode.UPDATErEPEAT);
                    return response;
                }

                drawcodeMapper.updatebatch(id,drawcode1);

//                }
               /* for ( String key : ) {
                    String id = key;
                    String drawCode = map.get(key);
                    return drawcodeMapper.updatebatch(id,drawcode);
                }*/
            }
//        return turnplateService.updatebatch(drawcode);
            response.setCode(RetCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("updatebatch"+e.getMessage());
        }
        return response;
    }

    @Override
    public Response listinquire(String grade) {
        Response response = new Response();

        try {


            List<Map<String, Object>> customer = turnplaterollcallMapper.list(grade);
            Integer totalNum = turnplaterollcallMapper.listAmount(grade);
            //PageResult<Map<String, Object>> pageList = new PageResult(customer,totalNum);
            response.setCode(RetCode.SUCCESS);
            response.put("totalNum", totalNum);
            response.put("customer",customer);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("listinquire" + e.getMessage());
        }
        return response;


    }

    @Override
    public Response img(String img, String id) {
        Response response=new Response();

        try {
            turnplateMapper.insertByimg(id,img);
            response.setCode(RetCode.UPLOADSUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("img"+e.getMessage());
        }


        return response;
    }


}